<template>
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="70" label-position="right">
        <FormItem label="客人名称" prop="guest_name">
            <Input type="text" v-model="formValidate.guest_name"></Input>
        </FormItem>       
        <FormItem label="联系电话">
            <Input type="text" v-model="formValidate.phone"></Input>
        </FormItem>
        <FormItem label="证件">
            <Input type="text" v-model="formValidate.certificate"></Input>
        </FormItem>         
        <FormItem label="业务员">
            <Select placeholder="选择业务员" v-model="formValidate.salesman_id">
                <Option v-for="item in salesmans" :value="item.id" :key="item.id">{{ item.cn_name }}</Option>                
            </Select>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit('formValidate')">确定</Button>
        </FormItem>
    </Form>
</template>
<script>

export default {
    name: 'guestinfo',
    props: ['formValidate'],
    data(){
        return {            
            salesmans:'',            
            ruleValidate:{
                guest_name:[
                    { required: true, message: '请输入客人名称', trigger: 'blur' }
                ]
            }       
        }
    },
    created(){        
        this.$axios({
            mothod: 'post',
            url: 'http://localhost/public/api/manager',
            params: ''
        }).then((response)=> {            
            this.salesmans = response.data.datas;
        })
        .catch(function (error) {
            console.log(error);
        }); 
    },
    methods: { 
        addGuest(){
            this.$axios({
                mothod: 'post',
                url: 'http://localhost/public/api/guest/create',
                params: this.formValidate
            }).then((response)=> { 
                this.$Message.success('录入成功!'); 
            })
            .catch(function (error) {
                this.$Message.error('录入失败!');
            });   
        },
        editGuest(){
            this.$axios({
                mothod: 'post',
                url: 'http://localhost/public/api/guest/update',
                params: this.formValidate
            }).then((response)=> { 
                this.$Message.success('编辑成功!'); 
            })
            .catch(function (error) {
                this.$Message.error('编辑失败!');
            });   
        },                 
        handleSubmit(name){                 
            this.$refs[name].validate((valid) => {                   
                if (valid) {                                 
                    if(this.formValidate.id != ""){
                        this.editGuest(); 
                    }else{
                        this.addGuest();                                 
                    }  
                    this.$emit('updateGuestList');    
                    this.$emit('closeDialog');              
                } else {
                    this.$Message.error('提交失败!');
                }
            })
        }
    }
}
</script>
